<?php
/**
 * vim: et ts=4 sts=4 sw=4 fdm=marker
 *
 * 邮件发送--基于phpmailer	 
 *
 * @author	wang Penghai <china_php@hotmail.com>
 * @copyright 2008 5e6d Corporation.
 * @version   $Id:$
 */

$root_path = dirname(__FILE__);
define('LIB_ROOT', $root_path);
include_once $root_path . '/phpmail/class.phpmailer.php';
class pf_lib_mail extends PHPMailer{

	/*{{{ member*/
	/*{{{ protected $_params*/

	/*
	 * 参数
	 */
	protected $_params = array(
			'host' => 'smtp.sina.com',
			'smtpdebug' => false,
			'smtpauth' => true,
			'port' => 25,
			'username' => 'wph45@sina.com',
			'password' => '208315224',
			'_from' => array(
				0 => array(
					'addr'=>'wph45@sina.com', 
					'name' => 'from',
					),
				),
			'_to' => array(//发件人
				0 => array(
					'addr'=>'wph45@sina.com', 
					'name' => 'to',
					),
				),
			);

	/*}}}*/
	/*}}}*/

	/*{{{ function*/
	/*{{{ function protected function _set_params()*/

	/**
	 * 设置参数
	 *
	 * @param $argvs = array()
	 *
	 * @return void
	 */

	protected function _set_params($argvs = array())
	{
		if (!empty($argvs) && is_array($argvs)) {
			$this->_params = array_merge($argvs, $this->_params);
		}
		
		$this->SMTPDebug  = $this->_params['smtpdebug']; // enables SMTP debug information (for testing)
		// 1 = errors and messages
		// 2 = messages only
		$this->SMTPAuth   = $this->_params['smtpauth']; // enable SMTP authentication
		$this->Host	   = $this->_params['host']; // sets the SMTP server
		$this->Port	   = $this->_params['port']; // set the SMTP port for the GMAIL server
		$this->Username   = $this->_params['username'];// SMTP account username
		$this->Password   = $this->_params['password'];// SMTP account password

		foreach ($this->_params['_to']  as $key => $val) {
			$this->AddReplyTo($val['addr'], $val['name']);
		}
		foreach ($this->_params['_to']  as $key => $val) {
			$this->SetFrom($val['addr'], $val['name']);
		}
		$this->PluginDir = LIB_ROOT . '/phpmail/';
		$this->CharSet = 'utf-8';
	}

	/*}}}*/
	/*{{{ function public function domail()*/
	
	/**
	 * 发邮件主函数
	 *
	 * @param 
	 *
	 * @return false | true
	 */
	public function domail($type='smtp', $argvs=array())
	{
		$this->_set_params($argvs);
		switch ($type) {
			case 'smtp':
				$this->IsSMTP();
				break;
			case 'sendmail':
			default:
				$this->IsSendmail();
				break;
		} 

		$res = $this->Send();
		return $res;
	}

	/*}}}*/
	/*}}}*/
}

/*测试
$mail = new pf_lib_mail();
$body = 'test';


$address = "wph45@163.com";
$mail->AddAddress($address, "addAddress");//收件人

$mail->Subject	= "PHPMailer Test Subject via Sendmail, basic";

$mail->AltBody	= "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

if ($mail->domail()) {
	echo 'true';
} else {
	echo 'false';
	echo $mail->ErrorInfo;
}
*/
